#!usr/bin/env python
#coding=utf8

from django.db import models

from base import UtilModel


class Item(models.Model, UtilModel):
    title = models.CharField(max_length=200, blank=False) 
    price = models.DecimalField(default=0, decimal_places=10, max_digits=23)
    tuanprice = models.DecimalField(default=0, decimal_places=10, max_digits=23, blank=True)
    createdtime = models.DateTimeField(auto_now_add=True) # 自动填充创建时间，更新不变
    #suppliers = models.ManyToManyField(Supplier, through='ItemLinkSupplier')
    #itemcategories = models.ManyToManyField(ItemCategory, through='ItemLinkItemCategory')
    class Meta:
        db_table = 'item'

class Supplier(models.Model, UtilModel):
    title = models.CharField(max_length=100, blank=False)
    items = models.ManyToManyField(Item, through='ItemLinkSupplier')
    class Meta:
        db_table = 'supplier'
        
class ItemLinkSupplier(models.Model, UtilModel):    
    item = models.ForeignKey(Item)
    supplier = models.ForeignKey(Supplier)
    class Meta:
        db_table = 'itemlinksupplier'

class ItemImage(models.Model, UtilModel):
    thumburl = models.CharField(max_length=1000, blank=False) 
    #item_id = models.IntegerField(default=0, blank=False)
    item = models.ForeignKey(Item)
    imagetype = models.CharField(max_length=10, blank=False) #home;minor
    originalurl = models.CharField(max_length=1000, blank=False) 
    class Meta:
        db_table = 'itemimage'

class TuanGouInfo(models.Model, UtilModel):
    tuan_salecount = models.IntegerField(default=0, blank=True)
    tuan_url = models.CharField(max_length=256, blank=True)
    tuan_saledate = models.DateTimeField(blank=True)
    tuan_price = models.DecimalField(default=0, decimal_places=10, max_digits=23, blank=True)
    tuan_sitename = models.CharField(max_length=256, blank=True)
    tuan_context = models.CharField(max_length=1000, blank=True)
    item = models.ForeignKey(Item)
    supplier = models.ForeignKey(Supplier)
    class Meta:
        db_table = 'tuangouinfo'

class ItemCategory(models.Model, UtilModel):
    title = models.CharField(max_length=100, blank=False)
    items = models.ManyToManyField(Item, through='ItemLinkItemCategory')
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children') #父分类
    level = models.IntegerField(default=0, blank=False) 
    index = models.IntegerField(default=0, blank=False) 
    class Meta:
        db_table = 'itemcategory'

class ItemLinkItemCategory(models.Model, UtilModel):
    item = models.ForeignKey(Item)
    itemcategory = models.ForeignKey(ItemCategory)
    class Meta:
        db_table = 'itemlinkitemcategory'

class AppForm(models.Model, UtilModel):
    title = models.CharField(max_length=200, blank=False)
    contact = models.CharField(max_length=200, blank=False)
    other = models.CharField(max_length=1000, blank=False)
    createdtime = models.DateTimeField(auto_now_add=True)
    class Meta:
        db_table = 'appform'

class Feedback(models.Model, UtilModel):
    context = models.CharField(max_length=1000, blank=False)
    createdtime = models.DateTimeField(auto_now_add=True)
    class Meta:
        db_table = 'feedback'

class HotItem(models.Model, UtilModel):
    item = models.ForeignKey(Item)
    index = models.IntegerField(default=0, blank=False) 
    class Meta:
        db_table = 'hotitem'

class SystemKeyValue(models.Model, UtilModel):
    key = models.CharField(max_length=100, blank=False)
    value = models.TextField() 
    class Meta:
        db_table = 'systemkeyvalue'           

class CooperationPlatform(models.Model, UtilModel):
    index = models.IntegerField(default=0, blank=False)
    url = models.CharField(max_length=256, blank=False)
    title = models.CharField(max_length=256, blank=False)
    imgurl = models.CharField(max_length=50, blank=True)
    showhomepage = models.BooleanField(default=False, blank=False)
    class Meta:
        db_table = 'cooperationplatform'

class CooperationBrand(models.Model, UtilModel):
    index = models.IntegerField(default=0, blank=False)
    url = models.CharField(max_length=256, blank=False)
    title = models.CharField(max_length=256, blank=False)
    imgurl = models.CharField(max_length=50, blank=True)
    showhomepage = models.BooleanField(default=False, blank=False)
    class Meta:
        db_table = 'cooperationbrand'

class ItemTuanGou(models.Model, UtilModel):
    title = models.CharField(max_length=200, blank=False) 
    price = models.DecimalField(default=0, decimal_places=10, max_digits=23)
    createdtime = models.DateTimeField()
    tuan_price = models.DecimalField(default=0, decimal_places=10, max_digits=23)
    tuan_salecount = models.IntegerField(default=0, blank=False)
    tuan_url = models.CharField(max_length=256, blank=True)
    tuan_saledate = models.DateTimeField()
    supplier_id = models.IntegerField(default=0, blank=False)
    item_id = models.IntegerField(default=0, blank=False)
    tuan_id = models.IntegerField(default=0, blank=False)


        
        